/*
 * Page页面 
 */

import Page from 'constants/page';
import { CALL_BOOK_API } from '../middlewares';

/**
 * 查询课时绘本导读数据
 * @param courseId 课程ID
 * @param lessonId 课时ID
 */
export function fetchBookGuide(lessonId: number) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Page.FETCH_BOOK_GUIDE, Page.FETCH_BOOK_GUIDE_SUCCESS, Page.FETCH_BOOK_GUIDE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/introduction`,
      method: 'GET',
    },
  };
}

/**
 * 绘本导读保存
 * @param lessonId 课时ID
 * @param data 绘本导读数据
 */
export function saveBookGuide(lessonId: number, data: Record<string, any>) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Page.SAVE_BOOK_GUIDE, Page.SAVE_BOOK_GUIDE_SUCCESS, Page.SAVE_BOOK_GUIDE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/introduction`,
      method: 'PUT',
      body: {
        ...data,
      },
    },
  };
}

/**
 * 创建page
 * @param lessonId
 * @param data
 */
export function createPage(lessonId: number, type: number, data: Record<string, any>) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Page.CREATE, Page.CREATE_SUCCESS, Page.CREATE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/pages`,
      method: 'POST',
      body: {
        ...data,
        type,
      },
    },
  };
}

/**
 * 保存Page页面（绘本页面，精读页面）
 * @param lessonId 课时ID
 * @param pageId 页面ID
 * @param data 页面数据
 */
export function savePage(lessonId: number, pageId: number, data: Record<string, any>) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Page.UPDATE, Page.UPDATE_SUCCESS, Page.UPDATE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/pages/${pageId}`,
      method: 'PUT',
      body: {
        ...data,
      },
    },
  };
}

/**
 * 删除Page页面（绘本页面，精读页面）
 * @param lessonId 课时ID
 * @param pageId 页面ID
 * @param data 页面数据
 */
export function removePage(lessonId: number, pageId: number) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Page.REMOVE, Page.REMOVE_SUCCESS, Page.REMOVE_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/pages/${pageId}`,
      method: 'DELETE',
    },
  };
}

/**
 * 查询课时绘本|精读页面列表
 * @param courseId 课程ID
 * @param lessonId 课时ID
 * @param type: 1:绘本，2:精读
 */
export function fetchPageList(lessonId: number, type: number) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Page.FETCH_LIST, Page.FETCH_LIST_SUCCESS, Page.FETCH_LIST_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/pages`,
      method: 'GET',
      body: {
        type,
      },
    },
  };
}

/**
 * 页面详情
 * @param lessonId
 * @param pageId
 */
export function fetchPageDetail(lessonId: number, pageId: number) {
  return {
    lessonId,
    [CALL_BOOK_API]: {
      types: [Page.FETCH, Page.FETCH_SUCCESS, Page.FETCH_FAIL],
      uri: `/course-api/a/lessons/${lessonId}/pages/${pageId}`,
      method: 'GET',
    },
  };
}
